package com.example.bbs.controller;

import com.example.bbs.entity.User;
import com.example.bbs.mapper.UserMapper;
import com.example.bbs.service.UserLogin;
import com.example.bbs.util.ConstUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2021/5/1 0:05
 */

@Controller
public class IndexController {

    @Autowired
    private UserLogin userLogin;

    @RequestMapping({"/", "/index"})
    public String defaultIndex(Model model) {
        return "index";
    }

    //成功登录状态
    @RequestMapping("/login")
    public String login(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            return "index";
        }
        return "index";
    }

    //登录验证 非管理员
    @RequestMapping("/tologin")
    @ResponseBody
    public String postToLogin(@RequestBody HashMap<String, String> paramMap, HttpSession session) {
        if (paramMap == null || paramMap.isEmpty()) {
            return "500";
        } else {
            return userLogin.checkLoginInfo(paramMap, session);
        }
    }

    //管理员验证
    @RequestMapping("/admin-tologin")
    @ResponseBody
    public String checkAdminLoginInfo(@RequestBody HashMap<String, String> paramMap, HttpSession session) {
        return userLogin.checkAdminLoginInfo(paramMap,session);
    }

    //注册验证
    @RequestMapping("/toregister")
    @ResponseBody
    public String postToRegister(@RequestBody HashMap<String, String> paramMap, HttpSession session) {
        if (paramMap == null || paramMap.isEmpty()) {
            return "500";
        } else {
            return userLogin.checkRegisterInfo(paramMap, session);
        }
    }

    @RequestMapping("/loginout")
    public String loginout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/";
    }

    @RequestMapping("/test")
    public String test(Model model) {
        return "inde1x";
    }


}
